<template>
  <div class="detail">
    <el-card>
      <el-tabs>
        <el-tab-pane label="登录账户设置">
          <el-form
            ref="form"
            class="isForm"
            label-width="350px"
            :model="form"
            :rules="rules"
          >
            <el-form-item label="姓名" prop="username">
              <el-input v-model="form.username" />
            </el-form-item>
            <el-form-item label="密码" prop="password">
              <el-input v-model="form.password" type="password" />
            </el-form-item>
            <el-form-item>
              <el-button type="primary" @click="submit">更改</el-button>
              <el-button>取消</el-button>
            </el-form-item>
          </el-form>
        </el-tab-pane>
        <el-tab-pane label="个人详情" lazy>
          <!-- <Info :user-info="initUserInfo" @setUserName="setUserName" /> -->
          <!-- 动态组件 -->
          <component
            :is="'Info'"
            :user-info="initUserInfo"
            @setUserName="setUserName"
          />
        </el-tab-pane>
        <el-tab-pane label="岗位信息" lazy>
          <!-- 动态组件 -->
          <component :is="'Job'" />
        </el-tab-pane>
      </el-tabs>
    </el-card>
  </div>
</template>

<script>
import { sysUser } from '@/api/user'
import { sysUserPut } from '@/api/employees'
// import Info from './components/info.vue'
export default {
  components: {
    // 异步组件
    // 异步组件与动态组件组合
    Info: () => import('./components/info.vue'),
    Job: () => import('./components/Job.vue')
  },
  data() {
    return {
      id: this.$route.params.id,
      userInfo: {},
      initUserInfo: {},
      form: {
        username: '',
        password: ''
      },
      rules: {
        username: [{ required: true, message: '必填', trigger: 'change' }],
        password: [
          { required: true, message: '必填', trigger: 'change' },
          { min: 6, max: 12, message: '请输入6-12位密码', trigger: 'change' }
        ]
      }
    }
  },
  created() {
    this.getData()
  },
  methods: {
    async getData() {
      const res = await sysUser(this.id)
      this.userInfo = res.data
      this.initUserInfo = JSON.parse(JSON.stringify(res.data))
      this.form.username = res.data.username
      this.userInfo = res.data
    },
    submit() {
      this.$refs.form.validate(async(result) => {
        if (result) {
          await sysUserPut({ ...this.userInfo, ...this.form })
          this.initUserInfo.username = this.form.username
          this.$message.success('修改成功')
        }
      })
    },
    setUserName(str) {
      this.form.username = str
    }
  }
}
</script>

<style lang="scss" scoped>
.detail {
  padding: 20px;
  .isForm {
    margin-top: 30px;
    width: 700px;
  }
}
</style>
